import sys
sys.path.append("/Users/ender/Code_repo/rosalind-brush-questions")
from utils import complement_dna, read_fasta


def get_kmers(sequence: str, kmer_length: int = 2):
    if kmer_length < 2:
        raise ValueError("K-mer's length must be more than 2")
    if kmer_length > len(sequence):
        raise ValueError("K-mer's length must be less than the length of sequence")

    i = 0
    kmers = []
    while i + kmer_length <= len(sequence):
        kmers.append(sequence[i:i+kmer_length])
        i += 1
    # kmers = set([sequence[i:i+kmer_length] for i in range(len(sequence) - kmer_length)])
    return set(kmers)


seq = read_fasta("./rosalind_revp.txt")
seq = seq.pop(list(seq.keys())[0])

seq1 = seq
seq2 = complement_dna(seq, reverse=True)

seq_kmers = set()
for kmer_length in range(4, 13):
    seq_kmers = seq_kmers.union(get_kmers(seq1, kmer_length))

palindromes = []
for kmer in seq_kmers:
    if kmer == complement_dna(kmer, reverse=True):
        palindromes.append(kmer)
print(palindromes)

for kmer in palindromes:
    for i in range(len(seq) - len(kmer) + 1):
        if seq[i:].startswith(kmer):
            print(i + 1, len(kmer))